#include "Renderer.hpp"

using namespace TheEmpires;

Renderer::Renderer(RenderWindow* _mWindow)
{
	mWindow				= _mWindow;
}

Renderer::~Renderer()
{

}

void Renderer::SetWindow(int width, int height)
{
	// Store the values for later use
	mWidth				= width;
	mHeight				= height;

	// Set the viewport & ortho & matrix mode
	glViewport(0, 0, mWidth, mHeight);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0f, mWidth, mHeight, 0.0f, -1.0f, 1.0f);

	// Disable some things we're not going to use here in a 2D game
	glDisable(GL_LIGHTING);
	glDisable(GL_DITHER);
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	// Enable some 2D things
	// Such as alpha blending and 2D textures
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_TEXTURE_2D);
}

bool Renderer::StartFrame()
{
	glClear(GL_COLOR_BUFFER_BIT);

	return true;
}

void Renderer::EndFrame()
{
	// Uh, SFML flips the buffers for us, I think
}

void Renderer::EnableClipper(Rectangle area)
{
	double left[4] = {1, 0, 0, -area.x};
	glClipPlane(GL_CLIP_PLANE0, left);
	glEnable(GL_CLIP_PLANE0);

	double top[4] = {0, 1, 0, -area.y};
	glClipPlane(GL_CLIP_PLANE1, top);
	glEnable(GL_CLIP_PLANE1);

	double right[4] = {-1, 0, 0, area.x2};
	glClipPlane(GL_CLIP_PLANE2, right);
	glEnable(GL_CLIP_PLANE2);

	double bottom[4] = {0, -1, 0, area.y2};
	glClipPlane(GL_CLIP_PLANE3, bottom);
	glEnable(GL_CLIP_PLANE3);
}

void Renderer::DisableClipper()
{
	glDisable(GL_CLIP_PLANE0);
	glDisable(GL_CLIP_PLANE1);
	glDisable(GL_CLIP_PLANE2);
	glDisable(GL_CLIP_PLANE3);
}

void Renderer::DrawRectangle(Color color, Vector2f position, Vector2f size, float rotation)
{
	/*
	if(rotation > 0)
	{
		glPushMatrix();

		glTranslatef(position.x + size.x / 2, position.y + size.y / 2, 0);
		glRotatef(rotation, 0, 0, 1);

		position			= Position(-size.x / 2, -size.y / 2);
	}

	glColor4f(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f);

	glBegin(GL_QUADS);
	{
		glVertex2i(position.x, position.y);
		glVertex2i(position.x + size.x, position.y);
		glVertex2i(position.x + size.x, position.y + size.y);
		glVertex2i(position.x, position.y + size.y);
	}
	glEnd();

	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

	if(rotation > 0)
	{
		glPopMatrix();
	}
	*/

	Shape shape			= Shape::Rectangle(position, position + size, color);

	if(rotation > 0)
	{
		shape.SetCenter(size / 2.0f);
		shape.SetRotation(rotation);
	}

	mWindow->Draw(shape);
}

void Renderer::DrawSprite(Sprite* sprite, Vector2f position, Vector2f size, IntRect renderarea, float rotation)
{
	if(sprite)
	{
		sprite->SetPosition(position);

		if(size.x > 0 && size.y > 0)
		{
			sprite->Resize(size);
		}

		if(rotation > 0)
		{
			sprite->SetCenter(size / 2.0f);
			sprite->SetRotation(rotation);
		}

		// TODO: Implement renderarea support

		mWindow->Draw(*sprite);
	}
}



